package com.rongshu.view.penpal.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.EditorInfo;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.rongshu.R;
import com.rongshu.view.base.BaseActivity;
import com.rongshu.view.base.mvp.INetView;
import com.rongshu.view.penpal.adapter.PenpalListAdapter;
import com.rongshu.view.penpal.bean.PenpalResponse;
import com.rongshu.view.penpal.present.PenpalSearchPresenter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.widget.EditTextWithDel;
import com.widget.titlebar.ImmersionBar;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * created by zhengweis on 2018/11/22.
 * description：笔友会搜索页面
 */
public class PenpalSearchActivity extends BaseActivity<PenpalSearchPresenter> implements INetView {
    @BindView(R.id.search_right_tv)
    TextView searchRightTv;
    @BindView(R.id.search_icon)
    ImageView searchIcon;
    @BindView(R.id.search_left_tv)
    EditTextWithDel searchLeftTv;
    @BindView(R.id.search_content_et)
    RelativeLayout searchContentEt;
    @BindView(R.id.penpal_list)
    RecyclerView penpalList;
    @BindView(R.id.refresh_view)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.no_data_layout)
    RelativeLayout noDataLayout;
    private int pageIndex = 1;
    private int pageSize = 10;
    private String msg;
    private List<PenpalResponse.ResponseBean> list;
    private PenpalListAdapter adapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ImmersionBar bar = ImmersionBar.with(this);
        bar.reset().keyboardEnable(true, WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE
                | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);  //软键盘自动弹出
        if (ImmersionBar.isSupportStatusBarDarkFont()) {//是否支持修改状态栏字体颜色
            bar.transparentStatusBar()  //透明状态栏，不写默认透明色
                    .statusBarDarkFont(true)   //状态栏字体是深色，不写默认为亮色
                    .flymeOSStatusBarFontColor(R.color.color_2c2c2c)  //修改flyme OS状态栏字体颜色
                    .statusBarAlpha(0);
        } else {
            bar.transparentStatusBar()  //透明状态栏，不写默认透明色
                    .flymeOSStatusBarFontColor(R.color.black)  //修改flyme OS状态栏字体颜色
                    //原理：如果当前设备支持状态栏字体变色，会设置状态栏字体为黑色，如果当前设备不支持状态栏字体变色，会使当前状态栏加上透明度，否则不执行透明度
                    .statusBarDarkFont(true, 1.0f);
        }
        bar.init();
    }

    @Override
    protected int initLayout() {
        return R.layout.activity_penpal_search;
    }

    @Override
    protected void initView() {
        searchLeftTv.setHint("请输入笔友会名称");
        refreshLayout.setEnableAutoLoadmore(false);
        list = new ArrayList<>();
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        penpalList.setLayoutManager(linearLayoutManager);
        adapter = new PenpalListAdapter(this, list);
        penpalList.setAdapter(adapter);

        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                pageIndex = 1;
                refreshlayout.resetNoMoreData();
                presenter.searchPenpalList(msg, pageIndex, pageSize);
                refreshlayout.finishRefresh(2000/*,false*/);//传入false表示刷新失败
            }
        });
        refreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                pageIndex++;
                presenter.searchPenpalList(msg, pageIndex, pageSize);
                refreshlayout.finishLoadmore(2000/*,false*/);//传入false表示刷新失败
            }
        });
    }

    @Override
    protected void initData() {
        setPresenter(new PenpalSearchPresenter(this), this);
        searchLeftTv.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    msg = v.getText().toString();
                    if (!TextUtils.isEmpty(msg)) {
                        showKeyboard(false);
                        presenter.searchPenpalList(msg, pageIndex, pageSize);
                    } else {
                        toast("请输入关键字");
                    }
                }
                return false;
            }
        });
    }

    @Override
    protected void updateViews(boolean isRefresh) {

    }

    @OnClick(R.id.search_right_tv)
    public void onViewClicked() {
        showKeyboard(false);
        finish();
    }

    @Override
    public void success(Object object, int tag) {
        PenpalResponse response = (PenpalResponse) object;
        if (response != null && response.getResponse() != null) {
            if (pageIndex == 1) {
                list.clear();
            }
            if (refreshLayout.isRefreshing()) {
                refreshLayout.finishRefresh();
            }
            if (refreshLayout.isLoading()) {
                refreshLayout.finishLoadmore();
            }
            list.addAll(response.getResponse());
            adapter.notifyDataSetChanged();
            if (response.getResponse().size() == 0) {
                refreshLayout.setLoadmoreFinished(true);
            }
            if (list.size() == 0) {
                noDataLayout.setVisibility(View.VISIBLE);

            } else {
                noDataLayout.setVisibility(View.GONE);

            }
        }
    }

    @Override
    public void faild(Object object, int tag) {

    }
}
